<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="活动名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入活动名称" />
      </el-form-item>
      <el-form-item label="活动时间" prop="activeDate">
        <el-date-picker
          v-model="formData.activeDate"
          type="date"
          value-format="x"
          placeholder="选择活动时间"
        />
      </el-form-item>
      <el-form-item label="活动开始时间段" prop="activeTimeBegin">
        <el-input v-model="formData.activeTimeBegin" placeholder="请输入活动开始时间段" />
      </el-form-item>
      <el-form-item label="活动结束时间段" prop="activeTimeEnd">
        <el-input v-model="formData.activeTimeEnd" placeholder="请输入活动结束时间段" />
      </el-form-item>
      <el-form-item label="男人数" prop="menNum">
        <el-input v-model="formData.menNum" placeholder="请输入男人数" />
      </el-form-item>
      <el-form-item label="女人数" prop="womenNum">
        <el-input v-model="formData.womenNum" placeholder="请输入女人数" />
      </el-form-item>
      <el-form-item label="负责人" prop="head">
        <el-input v-model="formData.head" placeholder="请输入负责人" />
      </el-form-item>
      <el-form-item label="套餐类型" prop="packageType">
        <el-select v-model="formData.packageType" placeholder="请选择套餐类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="套餐名称" prop="packageName">
        <el-input v-model="formData.packageName" placeholder="请输入套餐名称" />
      </el-form-item>
      <el-form-item label="活动费用" prop="activeCost">
        <el-input v-model="formData.activeCost" placeholder="请输入活动费用" />
      </el-form-item>
      <el-form-item label="截止时间" prop="deadline">
        <el-input v-model="formData.deadline" placeholder="请输入截止时间" />
      </el-form-item>
      <el-form-item label="活动描述" prop="activeDesc">
        <el-input v-model="formData.activeDesc" placeholder="请输入活动描述" />
      </el-form-item>
      <el-form-item label="活动封面" prop="activeCover">
        <el-input v-model="formData.activeCover" placeholder="请输入活动封面" />
      </el-form-item>
      <el-form-item label="审核状态（0未审核 1已通过 2已驳回）" prop="auditStatus">
        <el-radio-group v-model="formData.auditStatus">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="审核人" prop="auditUser">
        <el-input v-model="formData.auditUser" placeholder="请输入审核人" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
// import * as ActivityInfoApi from '@/api/blind/activityInfo'
import * as ActivityVerificateApi from '@/api/blind/activityVerificate'

const {t} = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  name: undefined,
  activeDate: undefined,
  activeTimeBegin: undefined,
  activeTimeEnd: undefined,
  menNum: undefined,
  womenNum: undefined,
  head: undefined,
  packageType: undefined,
  packageName: undefined,
  activeCost: undefined,
  deadline: undefined,
  activeDesc: undefined,
  activeCover: undefined,
  auditStatus: undefined,
  auditUser: undefined
})
const formRules = reactive({
  activeDate: [{ required: true, message: '活动时间不能为空', trigger: 'blur' }],
  deadline: [{ required: true, message: '截止时间不能为空', trigger: 'blur' }],
  activeCover: [{ required: true, message: '活动封面不能为空', trigger: 'blur' }],
  auditStatus: [{ required: true, message: '审核状态（0未审核 1已通过 2已驳回）不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ActivityVerificateApi.getActivityVerificate(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ActivityVerificateApi.ActivityVerificateVO
    if (formType.value === 'create') {
      await ActivityVerificateApi.createActivityVerificate(data)
      message.success(t('common.createSuccess'))
    } else {
      await ActivityVerificateApi.updateActivityVerificate(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    name: undefined,
    activeDate: undefined,
    activeTimeBegin: undefined,
    activeTimeEnd: undefined,
    menNum: undefined,
    womenNum: undefined,
    head: undefined,
    packageType: undefined,
    packageName: undefined,
    activeCost: undefined,
    deadline: undefined,
    activeDesc: undefined,
    activeCover: undefined,
    auditStatus: undefined,
    auditUser: undefined
  }
  formRef.value?.resetFields()
}
</script>
